package com.rich.dmr.ui.activity;


import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import com.rich.dmr.R;
import com.rich.dmr.ui.fragment.ChannelFragment;
import com.rich.dmr.ui.fragment.MainFragment;
import com.rich.dmr.utils.Log;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;
import rich.dmr.DmrManager;


public class MainActivity extends AppCompatActivity  implements ViewPager.OnPageChangeListener {

    private String TAG = "rich---MainActivity";
    private long firstTime = 0;
    private DmrManager dmrManager;
    private ViewPager viewPager;
    private ViewPagerAdapter viewPagerAdapter;
    @SuppressLint({"WrongConstant", "ClickableViewAccessibility"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dmrManager = (DmrManager) getSystemService("dmrservice");
        ArrayList<Fragment> list = new ArrayList<>();
        list.add(new ChannelFragment());
        list.add(new MainFragment());
        CircleIndicator circleIndicator;
        circleIndicator = findViewById(R.id.main_strategy_indicator);
        //circleIndicator.setVisibility(View.GONE);
        viewPager = findViewById(R.id.main_viewpager);
        viewPager.setOnPageChangeListener(this);
        viewPagerAdapter = new ViewPagerAdapter(getFragmentManager(),list);
        viewPager.setAdapter(viewPagerAdapter);
        circleIndicator.setViewPager(viewPager);
        viewPager.setCurrentItem(1);
    }

    public ViewPager getViewPager() {
        return viewPager;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                dmrManager.setMode(1);
                finish();
            } else {
                Toast.makeText(MainActivity.this, getString(R.string.press_again_back), Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int pageIndex) {
        Log.d(TAG,"onPageSelected pageIndex="+pageIndex);
        if(pageIndex == 0){

        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    public class ViewPagerAdapter extends FragmentPagerAdapter {
        List<Fragment> fragmentList;
        public ViewPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            fragmentList = list;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }
}
